package com.example.webmodule.controller;

import com.alibaba.fastjson.JSON;
import com.example.webmodule.feign.BaseApi;
import com.example.webmodule.feign.MyFeignClient;
import com.example.webmodule.vo.BodyData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@Tag(name = "用户管理", description = "用户相关的 API")
public class UserController {

    @Autowired
    private MyFeignClient myFeignClient;

    @Autowired
    BaseApi baseApi;

    @GetMapping("/users/{id}")
    @Operation(summary = "获取用户信息", description = "根据用户 ID 获取用户信息")
    @ApiResponse(responseCode = "200", description = "成功获取用户信息")
    public String getUser(
            @Parameter(description = "用户 ID", required = true, example = "1")
            @PathVariable Long id) {
        BodyData data = myFeignClient.getResource(id);
        return "User =" + JSON.toJSONString(data);
    }

    @GetMapping("/user")
    @Operation(summary = "获取用户信息", description = "根据用户 ID 获取用户信息")
    @ApiResponse(responseCode = "200", description = "成功获取用户信息")
    public String getUser2(
            @Parameter(description = "用户 ID", required = true, example = "1")
            @RequestParam Long id) {
        String str = baseApi.getUser(id);
        return "User =" + str;
    }

    @PostMapping("/users")
    @Operation(summary = "创建用户", description = "创建一个新用户")
    @ApiResponse(responseCode = "200", description = "用户创建成功")
    public String createUser(
            @Parameter(description = "用户名", required = true, example = "admin")
            @RequestParam String username) {
        return "User " + username + " created";
    }
}
